var table = layui.table;
var laydate = layui.laydate;
var form = layui.form;
$(document).ready(function() {
    //初始化数据列表
    load();

    //初始化右边工具栏
    initRightToolbar();
});

function load() {
    table.render({
        id: 'allInfoTable',
        elem: '#allInfoTable',
        method: 'post',
        url:'/ins/dept/dept_list',
        response: {
            statusCode: 200
        },
        cols: [[
            {field:'depName', title: '机构名称', sort: true},
            {field:'external', title: '是否外部机构',sort: true,templet:function(row){
                return row.external===0?'否':'是';
            }},
            {field:'depType', title: '机构类型', sort: true,templet:function(row){
                var str="";
                switch (row.depType){
                    case "1": str= "单位"; break;
                    case "2": str= "部门"; break;
                }
                return str;
            }},
            {field:'managerDep', title: '是否主管单位', sort: true,templet:function(row){
                    return row.managerDep===0?'否':'是';
                }},
            {field:'pname', title: '父机构名称', sort: true},
            {field:'respName', title: '负责人', sort: true},
            {field:'respPhone', title: '负责人电话', sort: true},
            {field:'linkName', title: '联系人', sort: true},
            {field:'linkPhone', title: '联系人电话', sort: true},
            {field:'zipcode', title: '邮编', sort: true},
            {field:'address', title: '单位地址', sort: true},
            {fixed: 'right', width: 120, align: 'center', unresize: true, toolbar: '#rightToolbar'}
        ]],
        page: true
    });

    $("#searchBtn").click(search);

    //监听操作
    form.on('switch(yesOrNo)', function(obj){
        var data={"id":this.value};
        data[this.name]=obj.elem.checked==true?1:0;
        debugger
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "/ins/dept/updateStatus",
            data: data,
            success: function (obj) {
                layer.msg("修改成功");
                //layer.alert("", {shade: [0.3, '#000'], offset: "t"});
            },
            error: function (data) {
                layer.alert("网络错误", {shade: [0.3, '#000'], offset: "t"});
            }
        });
        //layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
    });
}

function initRightToolbar() {
    table.on('tool(allInfoTable)', function(obj){
        /*if(obj.event === 'allInfo_detail') {
            allInfoDetail(obj);
        }
        if(obj.event === 'allInfo_del') {
            allInfoDel(obj);
        }*/
        if(obj.event === 'dept_edit') {
            edit(obj);
        }
        if(obj.event === 'dept_del') {
            del(obj);
        }
    });
}


/**
 * 新增按钮
 */
function add() {
    layer.open({
        type: 2,
        title: '新增',
        fix: false,
        resize: false,
        area: ['680px', '450px'],
        btn: ['保存', '关闭'],
        content: '/ins/dept/add',
        yes: function(index, layero) {
            $(window.frames[0].document).contents().find("#submitBtn").click();
        }
    });
}


/**
 * 编辑按钮
 */
function edit(obj) {
    layer.open({
        type: 2,
        title: '编辑',
        fix: false,
        resize: false,
        area: ['680px', '450px'],
        btn: ['保存', '关闭'],
        content: '/ins/dept/edit?id='+obj.data.id,
        yes: function(index, layero) {
            $(window.frames[0].document).contents().find("#submitBtn").click();
        }
    });
}
/**
 * 详情按钮
 * @param obj
 */
function allInfoDetail(obj) {
    layer.open({
        type: 2,
        title: '详情',
        fix: false,
        resize: false,
        area: ['680px', '400px'],
        btn: ['关闭'],
        content: '/all/info/toDetail/' + obj.data.id,
    });
}

/**
 * 删除按钮
 * @param obj
 */
function del(obj) {
    layer.confirm('确定删除数据吗？', function(index){
        $.get("/ins/dept/delete/" + obj.data.id, function(result) {
            if (200 == result.code) {
                layer.close(index);
                table.reload('allInfoTable');
            } else {
                layer.alert(result.msg);
            }
        });
    });
}

/**
 * 查询按钮
 */
function search() {
    table.reload('allInfoTable', {
        page: {
            curr: 1
        },
        where: $("#forms").serializeObject()
    });
}

